fix: register _fs_* macros for databases created via CREATE DATABASE#349
Open
mdz wants to merge 1 commit into
Open
fix: register _fs_* macros for databases created via CREATE DATABASE#349mdz wants to merge 1 commit into
mdz wants to merge 1 commit into
Conversation
The internal _fs_* macros (_fs_flatten, _fs_object_construct, _fs_to_timestamp) that sqlglot transforms depend on were only registered in the connection constructor, so databases created at runtime via CREATE DATABASE lacked them. This bit server mode, where a client connects once then issues CREATE DATABASE per catalog: the first TO_TIMESTAMP_NTZ / LATERAL FLATTEN against such a catalog failed with "_fs_to_timestamp does not exist" / "_fs_flatten does not exist". Run macros.creation_sql(catalog) on CREATE DATABASE execution, mirroring the connection constructor, so runtime-created catalogs get the same treatment as connect-time ones. Fixes tekumara#347 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Register the internal
_fs_*macros (_fs_flatten,_fs_object_construct,_fs_to_timestamp) when a database is created via a runtimeCREATE DATABASEstatement, not only in the connection constructor.Why
These macros are what sqlglot's transforms rewrite to, but
macros.creation_sql(catalog)was only invoked fromFakeSnowflakeConnection.__init__. A database created later viaCREATE DATABASE foonever had its macros registered.This is invisible in library mode (you connect with the target database, so its macros get created), but bites server mode: a client connects once, then issues
CREATE DATABASEper catalog. The firstTO_TIMESTAMP_NTZ(...)/LATERAL FLATTEN(INPUT => ...)against such a catalog failed with:Fixes #347.
How
In
cursor.py, thecreate_db_namebranch already raninfo_schema.per_db_creation_sql(...). This adds the matchingmacros.creation_sql(...)call right after it, mirroringconn.pyexactly. The macros useCREATE OR REPLACE, so re-running on an existing catalog (e.g.CREATE DATABASE IF NOT EXISTS) is idempotent.Test
test_create_database_registers_macroscreates a database at runtime and verifies bothTO_TIMESTAMP_NTZ(_fs_to_timestamp) andLATERAL FLATTEN(_fs_flatten) work against it.🤖 Generated with Claude Code